﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Psl.Remoting;
using Psl.Applications;
using Psl.Chat;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting;

namespace Stl.Chat.Remoting.Plugin
{
    public partial class ClientCluster : UserControl, IDisposable
    {
        // Champs
        protected static string ThreadSafeModel { get { return "ThreadSafeModel"; } }
        protected static string DefaultServer { get { return "localhost:50000"; } }
        protected static int DefaultPort { get { return 0; } }
        protected static Channels.Protocol DefaultProtocol { get { return Channels.Protocol.Http; } }
        protected static Channels.Format DefaultFormat { get { return Channels.Format.Soap; } }

        public string Server { get; protected set; }
        public int Port { get; protected set; }
        public Channels.Protocol Protocol { get; protected set; }
        public Channels.Format Format { get; protected set; }

        // Constructeur
        public ClientCluster()
        {
            InitializeComponent();

            Registry.MergeInMainMenu(menuStrip);
            Registry.MergeInMainTools(toolStrip);

            disconnectAction.Enabled = false;
            connectAction.Enabled = true;

            Server = DefaultServer;
            Port = DefaultPort;
            Protocol = DefaultProtocol;
            Format = DefaultFormat;
        }

        // Méthodes
        public void Connexion()
        {
            try
            {
                if (ChatRegistry.ChatState.Channel != null)
                    ChannelServices.UnregisterChannel(ChatRegistry.ChatState.Channel);
                ChatRegistry.ChatState.Channel = Channels.NewChannel(Port, Protocol, Format);
                string url = (Protocol == Channels.Protocol.Http) ? "http://" : "tcp://";
                url += Server + "/" + ThreadSafeModel;

                ChatRegistry.ChatState.ModelInternal = (IChat)Activator.GetObject(typeof(IChat), url);
                ChatRegistry.ChatState.Model = new ThreadUIProxy(ChatRegistry.ChatState.ModelInternal);

                ChatRegistry.ChatState.FireChatOpened();

                MessageBox.Show("Succes : Connection établie.");
                connectAction.Enabled = false;
                disconnectAction.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public void Disconnexion()
        {
            ChatRegistry.ChatState.FireChatClosing();

            ChatRegistry.ChatState.Model.Dispose();

            if (ChatRegistry.ChatState.Channel == null) return;
            ChannelServices.UnregisterChannel(ChatRegistry.ChatState.Channel);
            ChatRegistry.ChatState.Channel = null;

            ChatRegistry.ChatState.FireChatClosed();
            
            MessageBox.Show("Succes : Déconnection réussie.");
            disconnectAction.Enabled = false;
            connectAction.Enabled = true;
        }

        // Evenements
        private void connectAction_Execute(object sender, EventArgs e)
        {
            Connexion();
        }

        private void disconnectAction_Execute(object sender, EventArgs e)
        {
            Disconnexion();
        }

        private void configAction_Execute(object sender, EventArgs e)
        {
            using (ConfigDialog configDialog =
                         new ConfigDialog(DefaultServer, DefaultPort, DefaultProtocol, DefaultFormat,
                         Server, Port, Protocol, Format))
            {
                DialogResult res = configDialog.ShowDialog();
                if (res == DialogResult.OK)
                {
                    Server = configDialog.Server;
                    Port = configDialog.Port;
                    Protocol = configDialog.Protocol;
                    Format = configDialog.Format;

                    MessageBox.Show("Serveur configuré.");
                }
            }
        }

        private void showToolsAction_Execute(object sender, EventArgs e)
        {
            showToolsAction.Checked = !showToolsAction.Checked;
            toolStrip.Visible = showToolsAction.Checked;
        }

        private void applicationEvents_Archive(IArchiver sender)
        {
            sender.PushSection("Stl.Chat.Remoting.Plugin.ClientCluster");
            try
            {
                sender.ArchiveProperty("Checked", showToolsAction, false);

                sender.ArchiveProperty("Server", this, DefaultServer);
                sender.ArchiveProperty("Port", this, DefaultPort);
                sender.ArchiveProperty("Protocol", this, DefaultProtocol);
                sender.ArchiveProperty("Format", this, DefaultFormat);
            }
            finally { sender.PopSection(); }

            if (sender.IsReading)
                toolStrip.Visible = showToolsAction.Checked;
        }

        #region IDisposable Members

        void IDisposable.Dispose()
        {
            Disconnexion();
        }

        #endregion

        private void aboutclient_Execute(object sender, EventArgs e)
        {
            (new Psl.Applications.StandardAboutForm("Plugin permetant de charger une IHM de type client.", "Bo CAI, Fabrice Reynaud", null)).ShowDialog();
        }
    }
}
